
/** 
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
package com.tompai.datagather.plugin.berkeley;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.sleepycat.je.Cursor;
import com.sleepycat.je.CursorConfig;
import com.sleepycat.je.Database;
import com.sleepycat.je.DatabaseEntry;
import com.sleepycat.je.Environment;
import com.sleepycat.je.LockMode;
import com.sleepycat.je.OperationStatus;
import com.tompai.datagather.crawldb.Generator;
import com.tompai.datagather.model.CrawlDatum;

/**
* @author: tompai
* @createTime: 2018年4月28日 下午7:42:41
* @history:
* @version: v1.0
*/
public class BerkeleyGenerator extends Generator {

    public static final Logger LOG = LoggerFactory.getLogger(BerkeleyGenerator.class);

    Cursor cursor = null;
    Database crawldbDatabase = null;
    Environment env = null;


    public BerkeleyGenerator(Environment env) {
        this.env = env;
        crawldbDatabase = env.openDatabase(null, "crawldb", BerkeleyDBUtils.defaultDBConfig);
        cursor = crawldbDatabase.openCursor(null, CursorConfig.DEFAULT);
    }


    @Override
    public CrawlDatum nextWithoutFilter() throws Exception {
        if (cursor.getNext(key, value, LockMode.DEFAULT) == OperationStatus.SUCCESS) {
            CrawlDatum datum = BerkeleyDBUtils.createCrawlDatum(key, value);
            return datum;
        }else{
            return null;
        }
    }

    public void close() throws Exception {

        if (cursor != null) {
            cursor.close();
        }
//        cursor = null;
        if (crawldbDatabase != null) {
            crawldbDatabase.close();
        }
    }

    protected DatabaseEntry key = new DatabaseEntry();
    protected DatabaseEntry value = new DatabaseEntry();

//    @Override
//    public CrawlDatum next() {
//
//
//
//
//        while (true) {
//            if (cursor.getNext(key, value, LockMode.DEFAULT) == OperationStatus.SUCCESS) {
//
//                try {
//                    CrawlDatum datum = BerkeleyDBUtils.createCrawlDatum(key, value);
//                    if (datum.getStatus() == CrawlDatum.STATUS_DB_SUCCESS) {
//                        continue;
//                    } else {
//                        if (datum.getExecuteCount() > maxExecuteCount) {
//                            continue;
//                        }
//                        totalGenerate++;
//                        return datum;
//                    }
//                } catch (Exception ex) {
//                    LOG.info("Exception when generating", ex);
//                    continue;
//                }
//            } else {
//                return null;
//            }
//        }
//    }
//
//    @Override
//    public CrawlDatum nextWithoutFilter() {
//        if (cursor == null) {
//            crawldbDatabase = env.openDatabase(null, "crawldb", BerkeleyDBUtils.defaultDBConfig);
//            cursor = crawldbDatabase.openCursor(null, CursorConfig.DEFAULT);
//        }
//
//        if (cursor.getNext(key, value, LockMode.DEFAULT) == OperationStatus.SUCCESS){
//            CrawlDatum datum = BerkeleyDBUtils.createCrawlDatum(key, value);
//        }
//    }

   
}
